package com.maccuoi.letsgo;

import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.EditText;

class UserRegistryTask extends AsyncTask<String, Integer, String> {
	private Activity context;
	private String phone;
	private AsyncTask<String, Integer, String> task;

	public UserRegistryTask(Activity ctx, String fone) {
		context = ctx;
		phone = fone;
	}

	@Override
	protected String doInBackground(String... params) {
		Log.i("appinfo", "Dang dang ky");
		String url = context.getResources().getString(R.string.api_base_url)
				+ context.getResources().getString(
						R.string.api_user_registry_url);
		Log.i("appinfo", "loading " + url);
		String[] keys = { "phone" };
		String[] vals = { this.phone };
		String content = AppHttpRequest.post(url,
				PostDataCreator.create(keys, vals));
		Log.i("appinfo", "Content loaded:" + content);
		JSONObject jsonObj = null;

		try {
			jsonObj = new JSONObject(content);
			if (jsonObj.getInt("success") == 1) {

				// Registry success full;
				String token = jsonObj.getString("token");

				// Store token to preferences;
				SharedPreferences sharedPreferences;
				sharedPreferences = context.getSharedPreferences(context
						.getResources().getString(R.string.pref_key_app),
						Context.MODE_PRIVATE);
				Editor editor = sharedPreferences.edit();
				editor.putString("phone", phone);
				editor.putString("token", token);

				editor.apply();
			} else {
				 Log.i("appinfo", "Status ko = 1");
			}
			 Log.i("appinfo", "API msg " + jsonObj.getString("message"));
		} catch (JSONException e) {
			Log.i("appinfo", "Loi parse json");
			e.printStackTrace();
		}

		return null;
	}

}

class AppHttpRequest {
	public static String post(String url, List<NameValuePair> data) {
		String content = null;
		HttpEntity httpEntity = null;
		try {
			// Add your data
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost(url);

			httppost.setEntity(new UrlEncodedFormEntity(data));

			HttpResponse response = httpclient.execute(httppost);

			// content = response.toString();
			httpEntity = response.getEntity();
			content = EntityUtils.toString(httpEntity);
		} catch (ClientProtocolException e) {
			Log.i("appinfo", "error ClientProtocolException");
		} catch (IOException e) {
			Log.i("appinfo", "error IOException");
		}

		return content;
	}

	public static String get(String url) {
		String content = null;
		HttpEntity httpEntity = null;

		HttpClient httpClient = new DefaultHttpClient();
		HttpGet httpGet = new HttpGet(url);
		HttpResponse response = null;
		try {
			response = httpClient.execute(httpGet);
			content = response.toString();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return content;
	}
}

/**
 * HTTPPostTask(String URL);
 */
class AppHttpRequestTask extends AsyncTask<String, Integer, String> {
	private List<NameValuePair> postData;
	private String url;

	public AppHttpRequestTask(String u, List<NameValuePair> data) {
		postData = data;
		url = u;
	}

	public String doInBackground(String... params) {
		Log.i("appinfo", "Posting request");
		String responseContent = null;
		HttpEntity httpEntity = null;
		try {
			// Add your data
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost(url);

			httppost.setEntity(new UrlEncodedFormEntity(postData));

			HttpResponse response = httpclient.execute(httppost);

			responseContent = response.toString();
			httpEntity = response.getEntity();
			responseContent = EntityUtils.toString(httpEntity);
		} catch (ClientProtocolException e) {
			Log.i("appinfo", "error ClientProtocolException");
		} catch (IOException e) {
			Log.i("appinfo", "error IOException");
		}
		return responseContent;
	}
}

class PostDataCreator {
	public PostDataCreator() {

	}

	public static List<NameValuePair> create(String[] keys, String[] vals) {
		List<NameValuePair> postData = new ArrayList<NameValuePair>();
		for (int i = 0; i < keys.length; i++) {
			postData.add(new BasicNameValuePair(keys[i], vals[i]));
		}
		return postData;
	}

}